package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.entity.Report;
import com.a01_2021.thousandstimes.instance.service.IReportService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @Resource
    IReportService reportService;

    /**
     * 添加报告
     *
     * @param instanceId        实例id（用于验证是否为实例
     * @param token             jwt令牌（获取用户id
     * @param reportDescription 报告描述
     * @param reportTheme       报告主题
     * @param reportContent     报告内容
     * @param attachment        附件列表
     * @return AjaxResponse
     * @throws CustomException 不应抛出该异常
     */
    @PreAuthorize("hasAuthority('report-addReport')")
    @DS("#header.instanceid")
    @PostMapping("addReport")
    public AjaxResponse addReport(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, String reportDescription, String reportTheme, String reportContent, @RequestParam(name = "attachment") ArrayList<String> attachment) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        HashMap<String, Object> resultMap = reportService.addReport(userId, reportDescription, reportTheme, reportContent, attachment);
        return AjaxResponse.success(resultMap);
    }

    /**
     * 通过用户id跟报告id获取报告（不可查询其他人报告
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param reportId   报告id
     * @return AjaxResponse
     * @throws CustomException 当查询报告不属于该用户是抛出该异常
     */
    @PreAuthorize("hasAuthority('report-getReport')")
    @DS("#header.instanceid")
    @PostMapping("getReport")
    public AjaxResponse getReport(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, long reportId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        //查询数据并返回
        return AjaxResponse.success(reportService.getReport(userId, reportId));
    }

    /**
     * 通过用户id跟报告id获取报告（可查询其他人报告
     *
     * @param instanceId 实例id
     * @param userId     用户id
     * @param reportId   报告id
     * @return AjaxResponse
     * @throws CustomException 当用户不存在时抛出异常
     */
    @PreAuthorize("hasAuthority('report-getReportFromUser')")
    @DS("#header.instanceid")
    @PostMapping("getReportFromUser")
    public AjaxResponse getReportFromUser(@RequestHeader("instanceid") String instanceId, long userId, long reportId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        //查询数据并返回
        return AjaxResponse.success(reportService.getReportFromUser(userId, reportId));
    }

    /**
     * 查询该用户的所有报告
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse
     * @throws CustomException 应当不抛出该异常
     */
    @PreAuthorize("hasAuthority('report-getReports')")
    @DS("#header.instanceid")
    @PostMapping("getReports")
    public AjaxResponse getReports(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        //查询用户具有报告
        List<Report> resultList = reportService.getReports(userId);
        return AjaxResponse.success(resultList);
    }

    /**
     * 通过用户id查询报告
     *
     * @param instanceId 实例id
     * @param userId     用户id
     * @return Ajaxresponse
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @PreAuthorize("hasAuthority('report-getReportsFromUser')")
    @DS("#header.instanceid")
    @PostMapping("getReportsFromUser")
    public AjaxResponse getReportsFromUser(@RequestHeader("instanceid") String instanceId, long userId) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        List<Report> resultList = reportService.getReports(userId);
        return AjaxResponse.success(resultList);
    }

    /**
     * 通过报告id修改用户自己的报告
     *
     * @param instanceId        实例id
     * @param token             jwt令牌
     * @param reportId          报告id
     * @param reportDescription 报告描述
     * @param reportTheme       报告主题
     * @param reportContent     报告内容
     * @return AjaxResponse
     * @throws CustomException 当报告不存在，修改的报告不属于该用户时抛出
     */
    @PreAuthorize("hasAuthority('report-updateReport')")
    @DS("#header.instanceid")
    @PostMapping("updateReport")
    public AjaxResponse updateReport(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, long reportId, String reportDescription, String reportTheme, String reportContent) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SaaS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        Report report = reportService.updateReport(userId, reportId, reportDescription, reportTheme, reportContent);
        return AjaxResponse.success(report);
    }
}
